package com.cust.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cust.sky.constant.MessageConstant;
import com.cust.sky.constant.StatusConstant;
import com.cust.sky.context.BaseContext;
import com.cust.sky.dto.CategoryDTO;
import com.cust.sky.dto.CategoryPageQueryDTO;
import com.cust.sky.entity.Category;
import com.cust.sky.exception.DeletionNotAllowedException;
import com.cust.sky.mapper.CategoryMapper;
import com.cust.sky.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cust.sky.service.DishService;
import com.cust.sky.service.SetmealService;
import com.cust.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author lei
 * @since 2024-07-10
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增分类
     *
     * @param categoryDTO
     */
    @Override
    public void saveCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        //属性拷贝
        BeanUtils.copyProperties(categoryDTO, category);

        //分类状态默认为禁用状态0
        category.setStatus(StatusConstant.DISABLE);
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        baseMapper.insert(category);
    }

    /**
     * 分类条件分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public IPage getPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        int page = categoryPageQueryDTO.getPage();
        int pageSize = categoryPageQueryDTO.getPageSize();
        String name = categoryPageQueryDTO.getName();
        Integer type = categoryPageQueryDTO.getType();
        Page<Category> pageParam = new Page(page, pageSize);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(name)) {
            wrapper.eq(Category::getName, name);
        }
        if (type != null) {
            wrapper.eq(Category::getType, type);
        }
        wrapper.orderByDesc(Category::getUpdateTime);
        IPage<Category> categoryIPage = baseMapper.selectPage(pageParam, wrapper);
        return categoryIPage;
    }

    /**
     * 删除分类
     *
     * @param id
     */
    @Override
    public void deleteCategoryById(Long id) {
        //查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        Integer count = dishService.countByCategoryId(id);
        if (count > 0) {
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        count = setmealService.countByCategoryId(id);
        if (count > 0) {
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //删除分类数据
        baseMapper.deleteById(id);
    }

    /**
     * 修改分类
     *
     * @param categoryDTO
     */
    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);

        category.setUpdateUser(BaseContext.getCurrentId());

        baseMapper.updateById(category);
    }

    /**
     * 启用、禁用分类
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Category category = baseMapper.selectById(id);
        category.setStatus(status);
        baseMapper.updateById(category);
    }

    /**
     * 根据分类类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> findCategoryByType(Integer type) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getStatus, StatusConstant.ENABLE);
        if (type != null) {
            wrapper.eq(Category::getType, type);
        }
        wrapper.orderByAsc(Category::getSort);
        wrapper.orderByDesc(Category::getCreateTime);
        List<Category> categoryList = baseMapper.selectList(wrapper);
        return categoryList;
    }

    /**
     * 根据分类id查找分类名称
     *
     * @param categoryId
     * @return
     */
    @Override
    public String findCategoryNameById(Long categoryId) {
        Category category = baseMapper.selectById(categoryId);
        return category.getName();
    }
}
